//package com.example.zhinongbao.view;
//
//import android.os.Bundle;
//import android.widget.ImageView;
//
//import androidx.appcompat.app.AppCompatActivity;
//import androidx.fragment.app.Fragment;
//import androidx.fragment.app.FragmentManager;
//
//import com.example.zhinongbao.R;
//
//
//public class MainActivity extends AppCompatActivity {
//
//    private ImageView mIvFirst;
//
//    private FirstFragment mFirstFragment;
//    private Fragment mContentFragment;
//    private FragmentManager mFragmentManager;
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_dibu);
//        mIvFirst =findViewById(R.id.iv_tab_one);
//        initFragment();
//        changeFragment(mFirstFragment);
//        mFragmentManager.beginTransaction()
//                .add(R.id.fragment_content, mFirstFragment).commitAllowingStateLoss();
//        mContentFragment = mFirstFragment;
//        mIvFirst.setSelected(true);
//
//    }
//
//    private void initFragment() {
//        mFragmentManager = getSupportFragmentManager();
//        mFirstFragment = new FirstFragment();
//    }
//
//    public void changeFragment(Fragment to) {
//        if (mContentFragment != null && mContentFragment != to) {
//            if (!to.isAdded()) {
//                mFragmentManager.beginTransaction().hide(mContentFragment)
//                        .add(R.id.fragment_content, to).commitAllowingStateLoss();
//            } else {
//                mFragmentManager.beginTransaction().hide(mContentFragment)
//                        .show(to).commitAllowingStateLoss();
//            }
//            mContentFragment = to;
//        }
//    }
//}
package com.example.zhinongbao.view;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import android.annotation.SuppressLint;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.view.View;

import android.widget.LinearLayout;

import com.example.zhinongbao.R;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout mTabOne;
    private LinearLayout mTabTwo;
    private LinearLayout mTabAdd;
    private LinearLayout mTabMessage;
    private LinearLayout mTabMine;
    private FragmentHome mFirstFragment;
    private MineFragment mMineFragment;
    private Fragment mContentFragment;
    private FragmentManager mFragmentManager;

    private MessageFragment messagesFragment;

    private XTActivityFragment xtActivity;


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dibu);
        mTabOne = findViewById(R.id.ll_tab_one);
        mTabTwo = findViewById(R.id.ll_tab_two);
        mTabAdd = findViewById(R.id.ll_tab_add);
        mTabMessage = findViewById(R.id.ll_tab_message);
        mTabMine = findViewById(R.id.ll_tab_mine);
        mTabOne.setOnClickListener(this);
        mTabTwo.setOnClickListener(this);
        mTabAdd.setOnClickListener(this);
        mTabMessage.setOnClickListener(this);
        mTabMine.setOnClickListener(this);
        initFragment();
        changeFragment(mFirstFragment);
        mFragmentManager.beginTransaction()
                .add(R.id.fragment_content, mFirstFragment).commitAllowingStateLoss();
        mContentFragment = mFirstFragment;
        mTabOne.setSelected(true);
    }

    private void initFragment() {
        mFragmentManager = getSupportFragmentManager();
        mFirstFragment = new FragmentHome();
        mMineFragment = new MineFragment();
        // 初始化TabNewsFragment
        messagesFragment = new MessageFragment();// 初始化MessageFragment
        xtActivity = new XTActivityFragment();

    }

    public void changeFragment(Fragment to) {
        if (mContentFragment != null && mContentFragment != to) {
            if (!to.isAdded()) {
                mFragmentManager.beginTransaction().hide(mContentFragment)
                        .add(R.id.fragment_content, to).commitAllowingStateLoss();
            } else {
                mFragmentManager.beginTransaction().hide(mContentFragment)
                        .show(to).commitAllowingStateLoss();
            }
            mContentFragment = to;
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_tab_one) {
            mTabOne.setSelected(true);
            mTabTwo.setSelected(false);
            mTabAdd.setSelected(false);
            mTabMessage.setSelected(false);
            mTabMine.setSelected(false);
            changeFragment(mFirstFragment);
        } else if (id == R.id.ll_tab_two) {
            mTabOne.setSelected(false);
            mTabTwo.setSelected(true);
            mTabAdd.setSelected(false);
            mTabMessage.setSelected(false);
            mTabMine.setSelected(false);
            changeFragment(xtActivity);
            Log.d("TAG", "onClick:tabNewsFragment " + xtActivity);
        } else if (id == R.id.ll_tab_add) {
            Intent intent = new Intent(MainActivity.this, Publish_Paper.class);
            startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(
                    MainActivity.this).toBundle());
        } else if (id == R.id.ll_tab_message) {
            mTabOne.setSelected(false);
            mTabTwo.setSelected(false);
            mTabAdd.setSelected(false);
            mTabMessage.setSelected(true);
            mTabMine.setSelected(false);
            changeFragment(messagesFragment);

        } else if (id == R.id.ll_tab_mine) {
            mTabOne.setSelected(false);
            mTabTwo.setSelected(false);
            mTabAdd.setSelected(false);
            mTabMessage.setSelected(false);
            mTabMine.setSelected(true);
            changeFragment(mMineFragment);
        }
    }
}